package vista;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.LinkedList;

import vista.listeners.BotonVolverListener;


import modelo.auto.ElementoTaller;
import modelo.escenario.Taller;

import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import controlador.StreetRoadApplication;
import controlador.taller.ControladorTaller;

public class PanelComprar extends javax.swing.JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	private Taller taller;
	
    /** Creates new form NewJPanel */
    public PanelComprar(Taller taller) {
    	this.taller = taller;
        initComponents();
        mostrarUsuarioyDinero(); //muestra los labels de nombre de usuario y dinero q tiene
    }
    
    
    
    
 // carga un vector de Strings con los tipos
    private String[] cargarlista( Taller taller){
    	
    	//esto no deberia estar aqui, ver!
    	
    	LinkedList<ElementoTaller> listaTaller= taller.getListaComponentes();
    	int size= listaTaller.size();
    	Iterator <ElementoTaller> ite = listaTaller.iterator();
    	String[] tipos= new String[size];
		int i=0;
		while (ite.hasNext()){
			tipos[i]= ite.next().getTipo();
			i++;
		}
		return tipos;
    }
    
    
    ListSelectionListener lsl= new ListSelectionListener(){
    	public void valueChanged(ListSelectionEvent e) {

    		int indiceseleccionado= ((JList) e.getSource()).getSelectedIndex();
        	LinkedList<ElementoTaller> listaTaller= taller.getListaComponentes();
        	
        	//cambio label descripcion del componente
        	String descripcion = listaTaller.get(indiceseleccionado).getDescripcion();
        	labelDescripcion.setText(descripcion);
        	
        	//cambio el label del precio del componente seleccionado
        	
        	double precio= listaTaller.get(indiceseleccionado).getPrecio();
        	String mostrar= Double.toString(precio);
        	
        	labelPrecio.setText(precio+ " Algo$");
        	
        	
    	}
    };
    
    
    ActionListener listenerComprar= new ActionListener(){
    	public void actionPerformed(ActionEvent e) {
    		// 1. ver cual item de la Jlist esta seleccionado
    		int indiceSeleccionado =jList1.getSelectedIndex();
    		
    		//2. intentar comprarlo
    		ControladorTaller ct = new ControladorTaller(taller);
    		ct.comprarComponente(indiceSeleccionado);
    	
    		//3. actualizar dinero actual
    		mostrarUsuarioyDinero();
    		
    	}

    };
    
    // para mostrar el label de nombre de usuario actual y dinero disponible
    public void mostrarUsuarioyDinero(){
    	String nombreUsr= StreetRoadApplication.getInstancia().getNombreUsuarioActual();
    	labelNombre.setText(nombreUsr);
    	
    	double dineroUsr = StreetRoadApplication.getInstancia().getDineroUsuarioActual();
    	labelDinero.setText(Double.toString(dineroUsr)+ " Algo$");
    	
    	
    }
    
    public void actualizarLista(){
    	jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = cargarlista(taller) ;
            
             public int getSize() { return strings.length; }
             public Object getElementAt(int i) { return strings[i]; }
         });
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        labelDescripcion = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        labelPrecio = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        labelNombre = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        labelDinero = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(800, 600));

        // importante aqui se carga la Jlist
        actualizarLista();
        
        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(jList1);

        jLabel1.setText("Descripcion");

        jLabel3.setText("Vida Util:");

        jLabel4.setText("100");

        jLabel5.setText("Precio:");

        labelPrecio.setText("");
       // jLabel6.setSize(84, 17);

        jButton1.setText("Comprar");

        jButton2.setText("Volver");

        jLabel7.setText("Nombre:");

        labelNombre.setText("nombre");

        jLabel9.setText("Dinero:");

        labelDinero.setText("xx Algo$");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel5))
                                .addGap(27, 27, 27)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(labelPrecio, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jButton2)
                                .addComponent(labelDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 359, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(200, 200, 200)
                        .addComponent(jButton1)))
                .addContainerGap(193, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(492, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jLabel9))
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelNombre)
                    .addComponent(labelDinero))
                .addGap(166, 166, 166))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(labelNombre))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(labelDinero))
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(28, 28, 28)
                        .addComponent(labelDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(68, 68, 68)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGap(29, 29, 29)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(labelPrecio))
                        .addGap(63, 63, 63)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton2))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 435, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(65, Short.MAX_VALUE))
        );
        
        //Agrego Listeners
        
        jList1.setSelectedIndex(0);
        jList1.addListSelectionListener(lsl);
        jButton1.addActionListener(listenerComprar);
        jButton2.addActionListener(new BotonVolverListener());
        
        
    }// </editor-fold>
    
   
                      


    // Variables declaration - do not modify
    private javax.swing.JList jList1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel labelDinero;
    private javax.swing.JLabel labelDescripcion;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel labelPrecio;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel labelNombre;
    private javax.swing.JLabel jLabel9;

    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration

}